package com.axon.components.containers
{
	import com.axon.components.events.AxTabEvent;
	
	import flash.events.MouseEvent;
	
	import mx.containers.TabNavigator;
	import mx.controls.Button;
	import mx.core.Container;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ItemClickEvent;

	[Event( name="axChange", type = "com.axon.components.events.AxTabEvent" )]  // name must be equal to AxTabEvent.CHANGE

	public class AxTabNavigator extends TabNavigator
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function AxTabNavigator()
		{
			super();
			addEventListener( FlexEvent.CREATION_COMPLETE,	onCreationComplete);
			addEventListener( IndexChangedEvent.CHANGE,		onChange );
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function onCreationComplete(event: FlexEvent):void
		{
			tabBar.addEventListener( FlexEvent.UPDATE_COMPLETE, onUpdateComplete);
			tabBar.addEventListener( ItemClickEvent.ITEM_CLICK, onItemClickEvent);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function onUpdateComplete(event: FlexEvent):void
		{			
			for each (var button: Button in tabBar.getChildren())
			{			
				button.addEventListener( MouseEvent.MOUSE_OVER,	onMouseEvent);
				button.addEventListener( MouseEvent.MOUSE_OUT,	onMouseEvent);
				button.addEventListener( MouseEvent.MOUSE_DOWN,	onMouseEvent);
				button.addEventListener( MouseEvent.MOUSE_UP,	onMouseEvent);
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function onMouseEvent(event: MouseEvent): void
		{
			dispatchEvent(new AxTabEvent(AxTabEvent.MOUSE_ACTIVITY, event));
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function onItemClickEvent(event: ItemClickEvent): void
		{
			dispatchEvent(new AxTabEvent(AxTabEvent.ITEMCLICK_ACTIVITY, event));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function onChange(event: IndexChangedEvent): void
		{
			dispatchEvent(new AxTabEvent(AxTabEvent.CHANGE));
			
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set selectedChild(value: Container): void
		{
			if (value == selectedChild)
				return;

			super.selectedChild = value;
			dispatchEvent(new AxTabEvent(AxTabEvent.CHANGE));
		}
	
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set selectedIndex(value: int): void
		{
			if (value == selectedIndex)
				return; 
				
			super.selectedIndex = value;
			dispatchEvent(new AxTabEvent(AxTabEvent.CHANGE));
		}
	
	}
}